<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2017/6/5
 * Time: 上午9:38
 */

namespace app\apitemp\controller;

use app\common\controller\ActionLogs;
use app\common\model\Agent;
use app\common\model\Merchant;
use think\Config;

class Api
{
    public function api()
    {
//        try {
            $postData = file_get_contents('php://input');
//        return json_encode($postData);
            if ($postData) {
//                $postData=iconv('GB2312', 'UTF-8', $postData);
//                return json_encode($postData);
                if (is_json($postData)) {
                    $param1 = json_decode($postData, true);

                    $query_type = $param1['method'];
                    $param = json_decode($postData, true);
//                    $check = $this->checkparam(array_keys($param), $query_type);
                    $data = $this->api_query($param);
//                    if (isset($param['merchantid'])) {
//                        $s = $this->checkappid($param['appid'], $param['merchantid']);
//                        if ($s == "FAIL") {
//                            $data['return_code'] = "FAIL";
//                            $data['result_code'] = "FAIL";
//                            $data['result_msg'] = "appid与merchantid不匹配";
//                            $data['return_msg'] = "appid与merchantid不匹配";
//                            return json_encode($data);
//                        }
//                    }

//                    if ($check['code'] == "0") {
//                        unset($param1['sign']);
//                        $sign = getsign(uni_array($param1), "MD5", $param['appid']);

//                        if ($sign == $param['sign']) {
//                            $data = $this->api_query($param);
//
//                        } else {
//                            $data['return_code'] = "SUCCESS";
//                            $data['result_code'] = "FAIL";
//                            $data['result_msg'] = "签名错误";
//                            $data['return_msg'] = "签名错误" . $sign;
//                        }
//                    } else {
//                        $data['return_code'] = "SUCCESS";
//                        $data['result_code'] = "FAIL";
//                        $data['result_msg'] = $check['message'];
//                        $data['return_msg'] = $check['message'];
//                    }
                } else {
                    $data['return_code'] = "FAIL";
                    $data['result_code'] = "FAIL";
                    $data['result_msg'] = "数据格式错误";
                    $data['return_msg'] = "请传递正确的数据格式";
                }
            } else {
                $data['return_code'] = "FAIL";
                $data['result_code'] = "FAIL";
                $data['result_msg'] = "数据格式错误";
                $data['return_msg'] = "请传递正确的数据格式";
            }
//        } catch (\Exception $e) {
//            $data['return_code'] = "FAIL";
//            $data['result_code'] = "FAIL";
//            $data['result_msg'] = "系统错误";
//            $data['return_msg'] = "系统错误" . $e->getMessage();
//        }
        return json_encode($data);


    }

    private function checkparam($param, $type)
    {
        switch ($type) {
            case "carduserlist":
                $sdata = array('method', 'merchantid', 'source', 'page', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardreportlist":
                $sdata = array('method', 'merchantid', 'source', 'report_type', 'page', 'version', 'timestamp', 'signaturenonce');
                break;
            case "carduserreportlist":
                $sdata = array('method', 'code', 'merchantid', 'source', 'report_type', 'page', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardqreportlist":
                $sdata = array('method', 'merchantid', 'page', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "getcardqrcode":
                $sdata = array('method', 'merchantid', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "carduserinfo":
                $sdata = array('method', 'code', 'merchantid', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardqinfo":
                $sdata = array('method', 'code', 'merchantid', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardczgive":
                $sdata = array('method', 'merchantid', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "cardczgiveprice":
                $sdata = array('method', 'merchantid', 'csid', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbonusrec":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbonuscon":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardqcon":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbalancecon":
                $sdata = array('method', 'merchantid', 'card_code', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "cardbalancerec":
                $sdata = array('method', 'csid', 'merchantid', 'pay_cate', 'card_code', 'source', 'out_trade_no', 'device_id', 'num', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "pay":
                $sdata = array('method', 'merchantid', 'confirm', 'client_ip', 'auth_code', 'source', 'device_id', 'total_fee', 'pay_cate', 'version', 'timestamp', 'signaturenonce');
                break;
            case "orderquery":
                $sdata = array('method', 'merchantid', 'out_trade_no', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderrefund":
                $sdata = array('method', 'merchantid', 'out_trade_no', 'device_id', "source", 'refund_fee', 'refund_desc', 'version', 'timestamp', 'signaturenonce');
                break;
            case "orderrefundquery":
                $sdata = array('merchantid', 'refund_no', 'out_trade_no', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderinfo":
                $sdata = array('merchantid', 'out_trade_no', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderfee":
                $sdata = array('merchantid', 'out_trade_no', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "orderreverse":
                $sdata = array('merchantid', 'out_trade_no', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "paylog":
                $sdata = array('merchantid', 'start_time', 'end_time', 'page', "device_id", 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "paycount":
                $sdata = array('merchantid', 'start_time', 'end_time', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "banjie":
                $sdata = array('merchantid', 'start_time', 'end_time', "device_id", 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "clientbinding":
                $sdata = array('binding_code', 'device_code', 'version', 'source', 'timestamp', 'signaturenonce');
                break;
            case "clientbindingque":
                $sdata = array('device_code', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "clientupdata":
                $sdata = array('client_ver', "device_id", 'device_code', 'source', 'version', 'timestamp', 'signaturenonce');
                break;
            case "getprintinfo":
                $sdata = array("print_size", "print_type", "search_key", 'source', "device_id", 'device_code', 'version', 'timestamp', 'signaturenonce');
                break;
            case "login":
                $sdata = array('appid', 'username', 'password', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "machlist":
                $sdata = array('appid', "device_id", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
            case "machinfo":
                $sdata = array('appid', "device_id", 'merchantid', 'version', 'timestamp', 'source', 'signaturenonce');
                break;
        }
        $arr = array_diff($sdata, $param);
        $arr2 = implode(',', $arr);

        if (!empty($arr2)) {
            $arrs['code'] = "FAIL";
            $arrs['message'] = "缺少参数" . $arr2;
        } else {
            $arrs['code'] = "0";
            $arrs['message'] = "SUCCESS";

        }
        return $arrs;
    }

    public function param_query($param)
    {
        $type = $param['method'];
        switch ($type) {
            case "cardbalancerec":
                if ($param['pay_cate'] == "Cash" || $param['pay_cate'] == "Electronics") {
                    if ($param['pay_cate'] == "Electronics") {
                        if ($param['out_trade_no'] !== "") {
                            $arrs['code'] = "SUCCESS";
                            $arrs['message'] = "SUCCESS";

                        } else {
                            $arrs['code'] = "FAIL";
                            $arrs['message'] = "out_trade_no 参数不能为空";
                        }
                    } else {
                        $arrs['code'] = "SUCCESS";
                        $arrs['message'] = "SUCCESS";
                    }

                } else {
                    $arrs['code'] = "FAIL";
                    $arrs['message'] = "pay_cate 参数不正确";
                }
                break;
        }
        return $arrs;
    }

    public function api_query($array)
    {
        try {

            $type = $array['method'];
            switch ($type) {
                //会员列表
                case 'carduserlist':
                    $data = action('api/cardinfo/carduserlist', array($array));
                    break;
                //会员充值/消费记录
                case "cardreportlist":
                    $data = action('api/cardinfo/cardreportlist', array($array));
                    break;
                //会员卡单用户核销/充值记录
                case "carduserreportlist":
                    $data = action('api/cardinfo/carduserreportlist', array($array));
                    break;
                //卡券核销记录
                case "cardqreportlist":
                    $data = action('api/cardinfo/cardqreportlist', array($array));
                    break;
                //获取会员卡开卡二维码
                case "getcardqrcode":
                    $data = action('api/cardinfo/getcardqrcode', array($array));
                    break;
                //获取会员卡用户信息
                case "carduserinfo":
                    $data = action('apitemp/cardinfo/carduserinfo', array($array));
                    break;
                //获取卡券信息
                case "cardqinfo":
                    $data = action('apitemp/cardinfo/cardqinfo', array($array));
                    break;
                //获取充送规则
                case "cardczgive":
                    $data = action('apitemp/cardinfo/cardczgive', array($array));
                    break;
                //获取充送规则金额
                case "cardczgiveprice":
                    $data = action('apitemp/cardinfo/cardczgiveprice', array($array));
                    break;
                //会员卡积分充值
                case "cardbonusrec":
                    $data = action('apitemp/cards/cardbonusrec', array($array));
                    break;
                //会员卡积分消费
                case "cardbonuscon":
                    $data = action('apitemp/cards/cardbonuscon', array($array));
                    break;
                //卡券核销
                case "cardqcon":
                    $data = action('apitemp/cards/cardqcon', array($array));
                    break;
                //会员卡余额消费
                case "cardbalancecon":
                    $data = action('apitemp/cards/cardbalancecon', array($array));
                    break;
                //会员卡余额充值
                case "cardbalancerec":
                    $data = action('apitemp/cards/cardbalancerec', array($array));
                    break;
                //支付
                case "pay":
                    $data = action('apitemp/pay/getpay', array($array));
                    break;
                //订单检查
                case "orderquery":
                    $data = action('apitemp/pay/getcheckpay', array($array));
                    break;
                //订单退款
                case "orderrefund":
                    $data = action('apitemp/order/getorderrefund', array($array));
                    break;
                //订单退款查询
                case "orderrefundquery":
                    $data = action('apitemp/order/order_refund_query', array($array));
                    break;
                //订单详情
                case "orderinfo":
                    $data = action('apitemp/order/orderinfo', array($array));
                    break;
                //订单金额
                case "orderfee":
                    $data = action('apitemp/order/getorderprice', array($array));
                    break;
                //撤消订单
                case "orderreverse":
                    $data = action('apitemp/order/getorderreverse', array($array));
                    break;
                //交易记录
                case "paylog":
                    $data = action('apitemp/statis/paylog', array($array));
                    break;
                //交易统计
                case "paycount":
                    $data = action('apitemp/statis/paycount', array($array));
                    break;
                case "banjie":
                    $data = action('apitemp/statis/banjie', array($array));
                    break;
                case "clientbinding":
                    $data = action('apitemp/login/clientbinding', array($array));
                    break;
                case "clientbindingque":
                    $data = action('apitemp/login/clientbindingque', array($array));
                    break;
                case "clientupdata":
                    $data = action('apitemp/login/clientupdata', array($array));
                    break;
                case "getprintinfo":
                    $data = action('apitemp/prints/getprintinfo', array($array));
                    break;
                case "login":
                    $data = action('apitemp/login/login', array($array));
                    break;
                case "machlist":
                    $data = action('apitemp/login/machlist', array($array));
                    break;
                case "machinfo":
                    $data = action('apitemp/login/getmachinfo', array($array));
                    break;
            }
        } catch (\Exception $e) {
            $data['return_code'] = "SUCCESS";
            $data['result_code'] = "FAIL";
            $data['result_msg'] = "系统错误";
            $data['return_msg'] = "系统错误" . $e->getMessage();
        }
        $data['version'] = "2.0.0";
        $data['timestamp'] = time();
        $data['signaturenonce'] = MD5(rand(1000000, 9999999));
        $data['sign'] = getsign(uni_array($data), "MD5", $array['appid']);
        $logs['type'] = "apilog";
        $logs['up'] = $array;
        $logs['down'] = $data;
        $ActionLogs=new ActionLogs();
        $ActionLogs->writelog("osapi","clientlog","log","client:",$logs);
        return $data;
    }

    //判断appid是否与merchantid匹配
    private function checkappid($appid, $merchantid)
    {
        $map['sign_appid'] = $appid;
        $Agent = new Agent();
        $Agents = $Agent->where($map)->field('id')->select();
        foreach ($Agents as $k => $v) {
            $agentid[] = $v['id'];
        }
        $sid = implode(',', $agentid);
        $mab['agent'] = array('in', $sid);
        $mab['id'] = $merchantid;
        $Merchant = new Merchant();
        $mach = $Merchant->where($mab)->find();
        if ($mach) {
            return "SUCCESS";
        } else {
            return "FAIL";
        }


    }



}